#coding:utf-8
from django.template import RequestContext
from django.http import HttpResponse,HttpResponseRedirect
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import render
import json

import common.httpclient as http
import common.tips as tips
import common.modelAdminUser as modelAdminUser
import config.myurls as myurls
import common.function as myfc
import common.category as mycategory

import math

def list(req):
  '现金券配置'
  
  main_nav=[]
  main_nav.append({"url":"","name":"现金券配置"})
  context={}
  context['title'] = '-现金券配置'
  context['main_nav']=main_nav
  
  if modelAdminUser.getLoginUserId(req)==0:
    return HttpResponseRedirect('/login')
  
  url=myurls.ApiGetPrizecouponsList;
  
  param=[];
  rt=[]
  
  
  param.append(("p",req.GET.get("page",1)))
  param.append(("pageSize",req.GET.get("pageSize",10)))
  try:
    rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
    if rt['response_code'] != 1:
      return tips.error(req,rt['response_message'])
  except HTTPError:
    pass
  context['list']=rt['list']
  nav_list=[0]*math.ceil(int(rt['count'])/int(rt['pageSize']))
  paginator = Paginator(nav_list,1)
  current_page_num = req.GET.get('page',1)
  try:
    current_page = paginator.page(current_page_num)
  except PageNotAnInteger:
    current_page = paginator.page(1)
  except EmptyPage:
    current_page = paginator.page(paginator.num_pages)
  context['current_page']=current_page
  context['paginator']=paginator
  
  return render(req, 'prizecoupons_list.html', context)

def edit(req):
  '现金券配置编辑'
  
  
  id=0
  try:
    id=int(str(req.GET.get("id")))
  except ValueError:
    pass
  
  if id==0:
    return tips.error(req,"id不能为0","")
  
  main_nav=[]
  main_nav.append({"url":"/prizecoupons/","name":"银行限额列表"})
  main_nav.append({"url":"","name":"现金券配置编辑"})
  context={}
  context['title'] = '-现金券配置编辑'
  context['main_nav']=main_nav
  context['whether_or_not']=mycategory.getCategoryByName("whether_or_not",req)
  context['available_end_type']=mycategory.getCategoryByName("available_end_type_arr",req)
  
  
  url=myurls.ApiGetPrizecouponsInfo;
  
  param=[];
  param.append(("id",id))
  rt=[]
  try:
    rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
    if rt['response_code'] != 1:
      return tips.error(req,rt['response_message'])
  except HTTPError:
    pass
  context['info']=json.loads(rt['info'])
  return render(req, 'prizecoupons_edit.html', context)


def editpost(req):
  '提交编辑'
  
  rt={}
  rt['response_code']=0
  rt['response_message']="待提交"
  
  #return HttpResponse(json.dumps(rt),content_type="application/json")
  param=[]
  
  for key in req.POST:
    v=req.POST.get(key)
    if key.find("ttstamp~")==-1:
      param.append((key,v))
    else:
      if v != "":
        param.append((key.replace("ttstamp~",""),myfc.strToTimestamp(v)))
    
  if req.POST.get("available_end_type") == "1":
    if req.POST.get("ttstamp~available_end_dt") == "" or req.POST.get("ttstamp~available_start_dt") == "":
       rt['response_code']=-1
       rt['response_message']="可用时间必填"
       return tips.returnAjax(rt)
       
    if myfc.strToTimestamp(req.POST.get("ttstamp~available_end_dt")) < myfc.strToTimestamp(req.POST.get("ttstamp~available_start_dt")):
      return tips.error(req,"可用开始时间必须小于可用截止时间","")
  else:
    if req.POST.get("valid_days") == "" or req.POST.get("valid_days") == "0":
      rt['response_code']=-2
      rt['response_message']="领取后有效天数须大于0"
      return tips.returnAjax(rt)
    
  url=myurls.ApiUpdPrizecouponsInfo;
  try:
    rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
    if rt['response_code'] != 1:
      return tips.error(req,rt['response_message'])
  except HTTPError:
    pass
  #return HttpResponse(json.dumps(rt),content_type="application/json")
  return tips.returnAjax(rt)
  #return tips.success(req,"提交成功","/prizecoupons/edit/?id="+req.POST.get("id"))
  